package com.example.yibu.modelConfig.ascPool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/***
  * @Message 线程池创建配置类
  * Author liangxs
 * @Date 2024-12-11
 */

@Slf4j
@Configuration
@EnableAsync
public class AsyncPoolConfig implements AsyncConfigurer {


    /***
     * threadNamePrefix : 线程前缀名
     * corePoolSize : 核心线程数
     * maxPoolSize : 最大线程数
     * queueCapacity : 队列容量
     * keepAliveSeconds : 线程池维护线程所允许的空闲时间(单位秒)
     */



    /***
     *  默认线程池配置
     * @return
     */
    @Bean(name = "defaultThreadPoolTaskExecutor")
    public ThreadPoolTaskExecutor defaultThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = createThreadPoolTaskExecutor("DefaultThreadPool-", 5, 16, 50, 60);
        log.info("线程池defaultThreadPoolTaskExecutor创建成功");
        return executor;
    }

    /***
     * 任务线程池
     * @return
     */
    @Bean(name = "taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor executor = createThreadPoolTaskExecutor("taskExecutor-", 5, 16, 50, 60);
        log.info("线程池taskExecutor创建成功");
        return executor;
    }

    /***
     * 定时任务线程池
      * @return
     */
    @Bean(name = "scheduledExecutor")
    public ScheduledExecutorService scheduledExecutor() {
        // 设置核心线程数、最大线程数、线程前缀名等参数
        int corePoolSize = 5; // 核心线程数
        ThreadFactory threadFactory = new ThreadFactory() {
            private final AtomicInteger threadNumber = new AtomicInteger(1);
            private final String namePrefix = "ScheduledThreadPool-";

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, namePrefix + threadNumber.getAndIncrement());
                if (t.isDaemon()) {
                    t.setDaemon(false);
                }
                if (t.getPriority() != Thread.NORM_PRIORITY) {
                    t.setPriority(Thread.NORM_PRIORITY);
                }
                return t;
            }
        };

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(corePoolSize, threadFactory);
        log.info("线程池ScheduledExecutorService 创建成功");
        return executorService;
    }




    /***
     * 创建线程池
     * @param threadNamePrefix
     * @param corePoolSize
     * @param maxPoolSize
     * @param queueCapacity
     * @param keepAliveSeconds
     * @return
     */
    private ThreadPoolTaskExecutor createThreadPoolTaskExecutor(String threadNamePrefix, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }


    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new AsyncError();
    }

}




